// @ts-ignore
/* eslint-disable */
import { request } from '@umijs/max';
const apiPrefix = '/web';

/** 删除组件版 删除组件版本。 DELETE /v1/ota/package */
export async function componentPkgControllerImplDeleteComponentPackage(
  // 叠加生成的Param类型 (非body参数swagger默认没有生成对象)
  params: API.componentPkgControllerImplDeleteComponentPackageParams,
  options?: { [key: string]: any },
) {
  return request<API.ApiResultVoid>(`${apiPrefix}/v1/ota/package`, {
    method: 'DELETE',
    params: {
      ...params,
    },
    ...(options || {}),
  });
}

/** 启用、禁用升级包 启用、禁用 PUT /v1/ota/package/update */
export async function componentPkgControllerImplUpdatePkg(
  body: API.PkgQuery,
  options?: { [key: string]: any },
) {
  return request<API.ApiResultVoid>(`${apiPrefix}/v1/ota/package/update`, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
    ...(options || {}),
  });
}

/** 上传组件版本 上传组件版本，括版本信息、文件等。 POST /v1/ota/package/upload */
export async function componentPkgControllerImplUploadComponentPackage(
  body: {
    /** 组件编码 */
    componentCode: string;
    /** 版本号 */
    version: string;
  },
  file?: File,
  options?: { [key: string]: any },
) {
  const formData = new FormData();

  if (file) {
    formData.append('file', file);
  }

  Object.keys(body).forEach((ele) => {
    const item = (body as any)[ele];

    if (item !== undefined && item !== null) {
      if (typeof item === 'object' && !(item instanceof File)) {
        if (item instanceof Array) {
          item.forEach((f) => formData.append(ele, f || ''));
        } else {
          formData.append(ele, JSON.stringify(item));
        }
      } else {
        formData.append(ele, item);
      }
    }
  });

  return request<API.ApiResultVoid>(`${apiPrefix}/v1/ota/package/upload`, {
    method: 'POST',
    data: formData,
    requestType: 'form',
    ...(options || {}),
  });
}

/** 查询组件版本列表 查询组件版本列表。 GET /v1/ota/packages */
export async function componentPkgControllerImplListComponentPackages(
  // 叠加生成的Param类型 (非body参数swagger默认没有生成对象)
  params: API.componentPkgControllerImplListComponentPackagesParams,
  options?: { [key: string]: any },
) {
  return request<API.ApiResultPaginationComponentPackVO>(`${apiPrefix}/v1/ota/packages`, {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  });
}
